package com.atom.process.cnmuseum.bm.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.atom.common.VariablesConstant;
import com.atom.common.entity.HistoricTaskDO;
import com.atom.common.entity.RunningTaskDO;
import com.atom.common.remote.user.UserUtil;
import com.atom.common.util.PreConditionUtil;
import com.atom.process.cnmuseum.bm.tool.TaskDefinitions;
import com.atom.process.cnmuseum.bm.vo.request.GcxxRequestVO;
import com.atom.process.cnmuseum.bm.vo.request.RequestVO;
import com.atom.process.cnmuseum.bm.vo.request.WhrlxxxRequestVO;
import com.atom.process.cnmuseum.bm.vo.request.XzxxRequestVO;
import com.atom.process.cnmuseum.bm.vo.request.YxxxRequestVO;
import com.sinosoft.cnmuseum.utils.LogUtils;

@Service
public class LuruService extends AbstractService implements ServiceInterface {

	@Override
	public void save(RequestVO vo,GcxxRequestVO gcxxvo,XzxxRequestVO xzxxvo, YxxxRequestVO yxxxvo, WhrlxxxRequestVO whrlxxxvo, UserUtil user) {
		/* 参数校验 */
		paramsCheck(vo);

		String taskId = vo.getTaskId();
		String processInstanceId = vo.getProcessId();
		/* 处理流程数据 */
    	String onetitle = "录入" + gcxxvo.getCollection_code();
		// 更新ADO
		saveAdo(taskId, vo, gcxxvo, xzxxvo,yxxxvo,whrlxxxvo,user,onetitle);
		
		// 更新流程变量
		// 生成标题
		String title = "编目_" + gcxxvo.getCollection_code();
		activitiHandler.updateProcessInstanceName(taskId, processInstanceId, title);
	}

	@Override
	public void submit(RequestVO vo, UserUtil user) {
		/* 参数校验 */
		paramsCheck(vo);
		String taskId = vo.getTaskId();
		String mainProcessInstanceId = vo.getProcessId();
		// 完成任务
		Map<String, Object> var_complete = new HashMap<String, Object>();
		var_complete.put(VariablesConstant.PREVIOUS_ASSIGNEE, user.getUserid());
		// 提交录入或审核
		String next = vo.getNext();
		if (next.equals("bm")) {
			// 获取最后编目人ID
			HistoricTaskDO bmr = historicTaskHandler.getLastByProcessIdAndTaskKey(mainProcessInstanceId, TaskDefinitions.编目.getKey());
			String bmrid = bmr.getAssignee();
			var_complete.put("taskFlowType", next);
			var_complete.put("nextAssignee", bmrid);
		} else {
			String nextids = vo.getNextids();
			var_complete.put("taskFlowType", next);
			var_complete.put("nextAssignee", nextids);
		}
		
		// 更新PDO
		ADO2PDO(mainProcessInstanceId, vo.getTaskId());
		commentDao.saveComment(vo.getYourAdvice(), user, null, 0, taskId, mainProcessInstanceId, TaskDefinitions.录入.getKey());
		/* 处理流程数据 */
		// 完成任务/写入已办表/删除待办表
		List<RunningTaskDO> tasklist = runningTaskHandler.getListByTaskId(taskId);
		if(tasklist.size()>1){
			activitiHandler.completeBranchTask(taskId, var_complete);
		}else{
			activitiHandler.completeTask(taskId, var_complete);
		}
		
		//生成日志
        LogUtils logUtils = new LogUtils();
        String desc = "录入"+LogUtils.MbXtglSJBJ_SUBMIT_DESC;
        logUtils.addLog(String.valueOf(LogUtils.type_submit), LogUtils.model_sjbj, user.getUserid(), desc);
	}

	private void paramsCheck(RequestVO vo) {
		PreConditionUtil.checkNotNull(vo.getTaskId(), "taskId is null");
		PreConditionUtil.checkNotNull(vo.getProcessId(), "processId is null");
	}

}
